import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/login'
import Home from '@/views/home/home'

Vue.use(VueRouter)

const routes = [
  {
    name: 'login',
    path: '/login',
    component: Login
  },
  {
    name: 'home',
    path: '/home',
    component: Home,
    children: [
      {
        name: 'users',
        path: '/users',
        component: () => import('@/views/users/users')
      },
      {
        name: 'rights',
        path: '/rights',
        component: () => import('@/views/rights/right')
      },
       {
        name: 'roles',
        path: '/roles',
        component: () => import('@/views/rights/role')
      },
      {
        name:'goods',
        path:'/goods',
        component: ()=> import('@/views/goods/goodslist')
      },
      {
         name:'goodsadd',
         path:'/goodsadd',
         component:()=> import('@/views/goods/goodsadd')
      },
      {
        name:'params',
        path:'/params',
        component:()=> import('@/views/goods/cateparams')
      },
      {
        name:'categories',
        path:'/categories',
        component:()=> import('@/views/goods/goodscate')
      },
      {
        name:'orders',
        path:'/orders',
        component:()=> import('@/views/orders/order')
      },
      {
        name:'reports',
        path:'/reports',
        component:()=> import('@/views/reports/report')
      }
    ]
  },

  {
    path: '/',
    redirect: '/home'
  }
]
//路由守卫拦截，进行token判断


const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
   if( to.fullPath === '/login'){
     next()
   }else{
     const token = localStorage.getItem('token') 
     if( !token ){
        router.push('/login') 
        // next({name:'login'})
        // next("/login")
        return
     }
    //  不写这个无法正常跳转到新的页面
     next()
   }
})
export default router
